package com.carbon.notice.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import com.carbon.model.Result;
import com.carbon.notice.entity.CarbonnewsInfo;
import com.carbon.notice.entity.NoticeAppendix;
import com.carbon.notice.service.ICarbonnewsInfoService;
import com.carbon.notice.service.INoticeAppendixService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.IOUtils;
import org.jodconverter.DocumentConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 碳资讯信息 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-06-29
 */
@RestController
@RequestMapping("/notice/carbonnewsInfo")
public class CarbonnewsInfoController {

    @Autowired
    private ICarbonnewsInfoService carbonnewsInfoService;

    @Autowired
    private INoticeAppendixService noticeAppendixService;

     /**
     * @Description 新增资讯
     */
    @PostMapping("/add")
    public Result add(@RequestBody CarbonnewsInfo carbonnewsInfo){
        carbonnewsInfoService.insert(carbonnewsInfo);
        List<NoticeAppendix> appendixList = carbonnewsInfo.getAppendixList();
        //有资源附件
        if(!CollectionUtils.isEmpty(appendixList)){
            for(NoticeAppendix appendix:appendixList){
                appendix.setNoticeId(carbonnewsInfo.getId());
                noticeAppendixService.save(appendix);
            }
        }
        return new Result();
    }

     /**
     * @Description 修改资讯信息
     */
     @PostMapping("/update")
    public Result update(@RequestBody CarbonnewsInfo carbonnewsInfo){
         List<NoticeAppendix> appendixList = carbonnewsInfo.getAppendixList();
         if(!CollectionUtils.isEmpty(appendixList)){
             for(NoticeAppendix appendix:appendixList){
                 //如果附件没有id，则说明是新增；有id则为修改
                 if(StringUtils.isEmpty(appendix.getId())){
                     noticeAppendixService.save(appendix);
                 }else {
                     noticeAppendixService.updateById(appendix);
                 }
             }
         }
        return new Result(carbonnewsInfoService.updateById(carbonnewsInfo));
    }

     /**
     * @Description 根据ID获取资讯详情
     */
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable Integer id){
        //查询碳讯基本信息
        CarbonnewsInfo carbonnewsInfo = carbonnewsInfoService.getById(id);
        //查询资源附件
        List<NoticeAppendix> appendixList = noticeAppendixService.queryByNoticeId(id);
        if(!CollectionUtils.isEmpty(appendixList)){
            carbonnewsInfo.setAppendixList(appendixList);
        }
        return new Result(carbonnewsInfo);
    }

     /**
     * @Description 分页查询
     */
    @PostMapping("/queryPage")
    public Result queryPage(@RequestBody CarbonnewsInfo carbonnewsInfo){
        PageHelper.startPage(carbonnewsInfo.getPageNum(),carbonnewsInfo.getPageSize());
        List<CarbonnewsInfo> list = carbonnewsInfoService.queryPage(carbonnewsInfo);
        PageInfo pageInfo = new PageInfo(list);
        return new Result(pageInfo);
    }

}
